package com.x.uikit.channel.feed;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.x.uikit.link.XLink;
import com.x.uikit.link.XParams;
import com.x.uikit.multistateview.IFeedLoadMoreView;

public class XFeedAdapter extends RecyclerView.Adapter<XFeedAdapter.ViewHolder> implements XLink.CommandHandler {
    public static int LOAD_MORE_VIEW_ITEM_TYPE = 199999999;

    static class ViewHolder extends RecyclerView.ViewHolder {
        ViewHolder(@NonNull View card) {
            super(card);
            RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            card.setLayoutParams(lp);
        }
    }

    protected IXFeed.DataSource mDataSource;
    protected XLink.EventObserver mEventObserver;
    protected RecyclerView mRecyclerView;
    protected IXFeed.CardFactory mCardFactory;
    protected IFeedLoadMoreView mLoadMoreView;


    public void setCardFactory(IXFeed.CardFactory factory) {
        this.mCardFactory = factory;
    }

    public void setEventObserver(XLink.EventObserver observer) {
        this.mEventObserver = observer;
    }

    public void setLoadMoreView(IFeedLoadMoreView loadMoreView) {
        boolean needUpdate = getItemCount() > 0;
        this.mLoadMoreView = loadMoreView;
        if (needUpdate && mLoadMoreView != null) {
            notifyDataSetChanged();
        }
    }

    @Override
    public boolean handCommand(int id, XParams params, XParams result) {
        if (mRecyclerView != null) {
            int len = mRecyclerView.getChildCount();
            for (int i = 0; i < len; i++) {
                View view = mRecyclerView.getChildAt(i);
                if (view != null) {
                    RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(view);
                    if (holder.itemView instanceof AbstractCard) {
                        boolean handle = ((AbstractCard) holder.itemView).handCommand(id, params, result);
                        if (handle) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }


    public void setDataSource(IXFeed.DataSource dataSource) {
        this.mDataSource = dataSource;
    }

    public IXFeed.DataSource getDataSource() {
        return mDataSource;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == LOAD_MORE_VIEW_ITEM_TYPE) {
            View view = mLoadMoreView.getView();
            return new ViewHolder(view);
        }
        if (mCardFactory == null) {
            throw new IllegalArgumentException("card adapter's card factory has be set，Before onCreateViewHolder called");
        }
        AbstractCard card = mCardFactory.getCard(parent.getContext(), viewType);
        card.setObserver(mEventObserver);
        ViewHolder holder = new ViewHolder(card);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        if (hasLoadMoreItem() && position == getItemCount() - 1) {
            return LOAD_MORE_VIEW_ITEM_TYPE;
        } else {
            IXFeed.CardData data = getItem(position);
            if (data != null) {
                return data.getCardType();
            }
        }
        return 0;
    }

    private boolean hasLoadMoreItem() {
        return mLoadMoreView != null;
    }

    @Override

    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        mRecyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        mRecyclerView = null;
    }

    @Override
    public void onViewAttachedToWindow(@NonNull ViewHolder holder) {
        if (holder.itemView instanceof AbstractCard) {
            ((AbstractCard) holder.itemView).onAttached();
        } else if (mLoadMoreView != null && holder.itemView == mLoadMoreView.getView()) {
            View itemView=holder.itemView;
            if(itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams){
                ((StaggeredGridLayoutManager.LayoutParams) itemView.getLayoutParams()).setFullSpan(true);
            }
        }
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
        if (holder.itemView instanceof AbstractCard) {
            ((AbstractCard) holder.itemView).onDetached();
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (hasLoadMoreItem() && position == getItemCount() - 1) {

        } else {
            IXFeed.CardData data = getItem(position);
            if (holder.itemView instanceof AbstractCard) {
                ((AbstractCard) holder.itemView).bind(position, data);
            }
        }
    }

    public IXFeed.CardData getItem(int position) {
        if (mDataSource != null) {
            return mDataSource.getCardData(position);
        }
        return null;
    }

    @Override
    public int getItemCount() {
        int size = mDataSource != null ? mDataSource.getCount() : 0;
        if (mLoadMoreView != null) {
            size += 1;
        }
        return size;
    }


}
